﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using MediaToolkit.Model;
using VideoSpliter.Model;
using VideoSpliter.Util;

namespace VideoSpliter.ViewModel
{
    public class SplitVideoViewModel:ObservableObject
    {
        public Dispatcher Dispatcher;

        private string _videoPath;

        public string VideoPath
        {
            get { return _videoPath; }
            set { SetProperty(ref _videoPath, value); }
        }

        public RelayCommand SelectVideoCommand { get; set; }

        public RelayCommand SplitVideoCommand { get; set; }

        private MetaData _metadata;
        private string _interval;
        private int? _totalParts;
        private int? _processedParts;
        private double _totalToProcess=1;
        private double _currentProcessed;
        private bool _isRunning = false;
        private bool _isGettingThumbnail;
        private bool _isShowPlaceholder=true;

        public bool IsGettingThumbnail
        {
            get { return _isGettingThumbnail; }
            set { SetProperty(ref _isGettingThumbnail,value); }
        }

        public bool IsShowPlaceholder
        {
            get { return _isShowPlaceholder; }
            set {SetProperty(ref _isShowPlaceholder,value); }
        }

        public string Interval
        {
            get { return _interval; }
            set
            {
                SetProperty(ref _interval,value);
                SplitVideoCommand.RaiseCanExecuteChanged();
            }
        }

        public MetaData Metadata
        {
            get { return _metadata; }
            set { SetProperty(ref _metadata, value); }
        }

        public int? TotalParts
        {
            get { return _totalParts; }
            set { SetProperty(ref _totalParts,value); }
        }

        public int? ProcessedParts
        {
            get { return _processedParts; }
            set { SetProperty(ref _processedParts,value); }
        }

        public double TotalToProcess
        {
            get { return _totalToProcess; }
            set { SetProperty(ref _totalToProcess,value); }
        }

        public double CurrentProcessed
        {
            get { return _currentProcessed; }
            set { SetProperty(ref _currentProcessed,value);}
        }

        public bool IsRunning
        {
            get { return _isRunning; }
            private set { SetProperty(ref _isRunning,value);}
        }

        public SplitVideoViewModel()
        {
            SplitVideoCommand = new RelayCommand((_)
                =>
            this.Dispatcher.Invoke(async()=>
                {
                    IsRunning = true;
                    SplitVideoCommand.RaiseCanExecuteChanged();
                        VideoSplitOperation operation = new VideoSplitOperation(
                        TimeSpan.FromMinutes(double.Parse(Interval)),Metadata

                        );
                    await Task.Run(() =>
                    {
                        operation.ProgressUpdated += Operation_ProgressUpdated;
                        operation.Split();
                        operation.ProgressUpdated -= Operation_ProgressUpdated;
                    });
                      
                    IsRunning = false;
                    Process.Start("Explorer.exe", operation.OutputDirectory);
                    TotalToProcess = 1;
                    TotalParts = null;
                    CurrentProcessed = 0;
                    ProcessedParts = null;
                    SplitVideoCommand.RaiseCanExecuteChanged();
                }
                )
            ,
            () =>  !(string.IsNullOrEmpty(Interval) || Metadata == null ||IsRunning==true)
            );

            SelectVideoCommand = new RelayCommand(async (_) =>

            {
                await Task.Run(
                    () =>
                    {
                        OpenFileDialog fd = new OpenFileDialog
                        {
                            Title = "选择视频",
                            Filter = "支持的视频文件|*.wma;*.avi;*.mpg;*.mp4;*.rmvb;*.flv;*.3gp;*.mov;*.mkv;*.vob"
                        };
                        var result = fd.ShowDialog();
                        if (result == true)
                        {
                            IsShowPlaceholder = false;
                            IsGettingThumbnail = true;
                            Metadata = null;
                            VideoPath = fd.FileName;
                            Metadata = MetaData.Read(VideoPath);
                            IsGettingThumbnail = false;

                        }
                    }
                ).ConfigureAwait(false);
                Dispatcher.Invoke(() =>
                    SplitVideoCommand.RaiseCanExecuteChanged());

            });


        }

        private void Operation_ProgressUpdated(object sender, ProgressEventArg e)
        {
            TotalParts = e.TotalParts;
            ProcessedParts = e.PartProcessing;
            TotalToProcess = e.TotalToProcess;
            CurrentProcessed = e.CurrentProcessed;
        }
    }
}
